package com.ccnf.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ccnf.core.utils.SystemClock;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

@Slf4j
public class SQLMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("新增数据: " + metaObject.getOriginalObject().getClass().getSimpleName());
        handleFillFieldValue(metaObject);
    }

    private void handleFillFieldValue(MetaObject metaObject) {
        setFieldValByName("createTime", SystemClock.second(), metaObject);
        setFieldValByName("updateTime", SystemClock.second(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("更新数据: " + metaObject.getOriginalObject().getClass().getSimpleName());
        setFieldValByName("updateTime", SystemClock.second(), metaObject);
    }
}